home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-04-12 | 4.5 KB | 157 lines | [TEXT/GEOL] |
- Item 4250386 12-April-89 09:38
-
- From: UK0004 Bacchus & Smith, Paul Smith
-
- To: MACAPP.TECH$ MACAPP Tech
- MACDTS Macintosh Developer Technical Supt.
-
- Sub: Re Dangerous Lock in 2.0b7
-
-
- DISCLAIMER: Let me start by saying that we do not have MacApp 2.0b7: we are
- still using MacApp 2.0b5, and my comments a solely as a result of seeing the
- link from Hermes Systems SA (Guy Fiems and Philippe Lange) this afternoon.
-
- I understand, from Hermes' link, that MacApp 2.0b7 implements "TObject.Lock"
- and ".Unlock" methods. This is BRILLIANT! We have used ".Lock" and ".Unlock"
- overrides ourselves since early betas of MacApp 1.0.
-
- Like Hermes, I think the MacApp 2.0b7 implementation is dangerous. I would go
- further, and say that, in my opinion, it positively invites disaster. BUT, I
- _absolutely_ disagree with Hermes' suggested solution. There is a better way.
-
- The better way, as described below, allows ANY NUMBER (up to maxint) of nested
- locks and unlocks, as long as they balance out in the end.
-
- In my view, the best way to implement ".Lock" and ".Unlock" is by keeping a
- Lock count (somewhat like traditional Semaphores, as used for synchronising
- sequential processes, access to shared resources, etc, etc).
-
- The basic principle is this: the lock counter variable keeps a record of how
- many times the object has been locked; it starts off at zero, and is
- incremented whenever the object is locked, and decremented whenever the object
- is unlocked; the object is only actually locked or unlocked when the lock count
- changes from zero to one or vice versa.
-
- Here's another explanation (more like pseudocode):
-
- • an object field is declared, called fLockCount, initialised to 0
-
- • to lock something, you call a routine that does this:
-
- if fLockCount equals 0, then LOCK the object;
- add 1 to fLockCount;
-
- • to unlock the object, you call a routine that does this:
-
- subtract 1 from fLockCount;
- if fLockCount equals 0, then UNLOCK the object;
-
- As I said above, we have been using this technique successfully for some time.
- I have pulled the relevant object declarations and code from our re-usable
- source code library and included it below.
-
- Maybe this sounds rather pig headed, but: If MacApp 2.0 final does not
- implement something like the code below, I will certainly NOT let my
- development engineers use the standard methods - we shall continue to override
- TObject with our own TLObject overrides.
-
- Of course, if (I say, hopefully) the gentlemen at Hermes are mistaken about
- what 2.0b7 actually does in this respect, then all's well and good
-
- Best regards,
-
- Paul G Smith
- ============
-
-
-
- { sample code follows }
-
- { ---------------------------------------------------------------------- }
- { declaration of TLObject class (from INTERFACE) }
-
- TLObject = OBJECT (TObject) { Lockable object }
-
- fLockCount : Integer; { Lock count, incremented when the
- object is locked. }
-
- Procedure TLObject.ILObject; { Initialise object (Set fLockcount to
- 0) }
-
- Procedure TLObject.Free; OVERRIDE; { Free object }
-
- Procedure TLObject.Lock; { Lock object }
-
- Procedure TLObject.UnLock; { Unlock object }
-
- {$IFC qDebug}
- Procedure TLObject.Inspect; OVERRIDE; { Writes out lock count }
- {$ENDC}
-
- end; { TLObject }
-
-
- { TLObject methods ------------------------------------------------------ }
- { from IMPLEMENTATION }
-
- {$S ARes}
-
- Procedure TLObject.ILObject;
- { Initialise object (Set fLockcount to 0) }
- Begin
- fLockCount := 0;
- End;
-
-
- Procedure TLObject.Free; OVERRIDE;
- { Free object }
- Begin
- {$IFC qDebug}
- If fLockCount <> 0 then
- ProgramBreak ('TLObject.Free : Lock count is not 0');
- {$ENDC}
- INHERITED Free;
- End;
-
-
- Procedure TLObject.Lock;
- { Lock object }
- Begin
- if fLockCount = 0 then
- begin
- HLock (Handle (SELF));
- end;
- fLockCount := fLockCount + 1;
- End;
-
-
- Procedure TLObject.UnLock;
- { Unlock object }
- Begin
- {$IFC qDebug}
- If fLockCount = 0 then
- begin
- ProgramBreak ('TLObject.UnLock : Lock count equals 0');
- end
- else
- {$ENDC}
- fLockCount := fLockCount - 1;
- if fLockCount = 0 then
- HUnLock (Handle (SELF));
- End;
-
-
- {$IFC qDebug}
- Procedure TLObject.Inspect; OVERRIDE;
- Begin
- INHERITED Inspect;
- Writeln(' fLockCount = ', fLockCount:1);
- End;
- {$ENDC}
-
- { ---------------------------------------------------------------------- }
-
-
-
-